package com.zxbit.uangstore.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zxbit.uangstore.R;
import com.zxbit.uangstore.utils.StringUtils;

/**
 * Created by wangxudong on 15/10/10.
 */
public class ViewUpSearch extends LinearLayout {


    private View round;
    private View circle;
    private EditText input;
    OnSearchListener listener;

    private boolean isExpand;
    private float scale;


    public ViewUpSearch(Context context) {
        super(context);
        initView(context);
    }

    public ViewUpSearch(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.view_choice_search_layout, this, true);
        round = findViewById(R.id.round);
        circle = findViewById(R.id.circle);
        ImageView search = findViewById(R.id.search);
        input = findViewById(R.id.input);
        updateShow(false);

        search.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null && !StringUtils.isEmpty(input.getText().toString())) {
                    listener.onSearchClick();
                } else {
                    updateShow(!isExpand);
                }
            }
        });
    }

    public String getText() {
        return input.getText().toString();
    }

    public void updateShow(boolean isExpand) {
        this.isExpand = isExpand;
        double roundWid = round.getWidth() / 1.0;
        double circleWid = circle.getWidth() / 1.0;
        scale = (float) (circleWid / roundWid);

        if (isExpand) {
            expandSearch();
        } else {
            closeSearch();
        }
    }

    private void expandSearch() {

        ObjectAnimator anim1 = ObjectAnimator.ofFloat(circle, "alpha", 1f, 0f);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(round, "alpha", 0f, 1f);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(round, "scaleX", scale, 1f);
        round.setPivotX(round.getWidth());
        AnimatorSet animSet2= new AnimatorSet();
        animSet2.play(anim1).with(anim2).with(anim3);
        animSet2.setDuration(250);
        animSet2.start();
        input.setVisibility(VISIBLE);
    }

    private void closeSearch() {
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(round, "scaleX", 1f, scale);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(circle, "alpha", 0f, 1f);
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(round, "alpha", 1f, 0f);
        round.setPivotX(round.getWidth());
        AnimatorSet animSet1 = new AnimatorSet();
        animSet1.play(anim2).with(anim3).with(anim4);
        animSet1.setDuration(250);
        animSet1.start();
        input.setVisibility(GONE);
    }

    public void setListener(OnSearchListener listener) {
        this.listener = listener;
    }

    public interface OnSearchListener {
        void onSearchClick();
    }
}
